package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.adapter.receive.ReceiveListAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveDeleteReceiveBean;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveDeliveryOverviewBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveDeliveryOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;

public class ReceiveDeliveryOverviewActivity extends BaseActivity {

    private String SUCCESS = "1";

    private String ERROR = "2";

    private TextView title, createReceiveNumber;

    private String deliveryNumberStr;

    private PullToRefreshLayout refresh;

    private PullableListView receiveList;

    private List<ReceiveDeliveryOverviewBean.DataBean.ReceiveListBean> allData;

    private ReceiveListAdapter adapter;

    private static int REQUEST_CODE = 100;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_delivery_overview);
        initView(ReceiveDeliveryOverviewActivity.this);
        initData(ReceiveDeliveryOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refresh = (PullToRefreshLayout) findViewById(R.id.receiveRefresh);
        receiveList = (PullableListView) activity.findViewById(R.id.receiveList);
        createReceiveNumber = (TextView) activity.findViewById(R.id.createReceiveNumber);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_delivery_overview_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiveList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                initData(activity);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        deliveryNumberStr = activity.getIntent().getStringExtra(ReceiveExtra.DELIVERY_NUMBER);
        getData(activity, deliveryNumberStr);

        createReceiveNumber.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(activity, ReceiveReceiveItemActivity.class);
                intent.putExtra(ReceiveExtra.DELIVERY_NUMBER, deliveryNumberStr);
                intent.putExtra(ReceiveExtra.RECEIVE_NUMBER, "");
                intent.putExtra(ReceiveExtra.IS_INSERT, true);
                activity.startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    private void addReceiveListData(Context context, ReceiveDeliveryOverviewBean.DataBean data, List<ReceiveDeliveryOverviewBean.DataBean.ReceiveListBean> receiveItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ReceiveListAdapter(context, data, receiveItemList, (ReceiveDeleteReceiveBean bean, Dialog dialog) -> {
                dialog.cancel();
                Utils.middleToast(context, bean.getData().getMsg());
                if (SUCCESS.equals(bean.getData().getCode())) {
                    initData(ReceiveDeliveryOverviewActivity.this);
                }

            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String deliveryNumberStr) {

        ReceiveDeliveryOverviewRequest.request(context, deliveryNumberStr, new GsonBeanListener<ReceiveDeliveryOverviewBean>() {
            @Override
            public void onSuccess(ReceiveDeliveryOverviewBean bean) {
                ReceiveDeliveryOverviewBean.DataBean data = bean.getData();
                allData = data.getReceiveList();
                addReceiveListData(context, data, ActivityUtils.getShowData(allData, 0), receiveList, true);
            }
        });
    }

    public interface ReceiveDeliveryOverviewListener {
        void onRequestSuccess(ReceiveDeleteReceiveBean bean, Dialog dialog);
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        if (REQUEST_CODE == requestCode && ReceiveExtra.CREATE_CREATE_NO_GOODSLIST == resultCode) {
            Utils.middleToast(ReceiveDeliveryOverviewActivity.this, ReceiveDeliveryOverviewActivity.this.getResources().getString(R.string.activity_receive_delivery_overview_no_materiel));
            createReceiveNumber.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.middleToast(ReceiveDeliveryOverviewActivity.this, ReceiveDeliveryOverviewActivity.this.getResources().getString(R.string.activity_receive_delivery_overview_no_materiel));
                }
            });
        }
    }
}
